
package carmadness;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import carmadness.model.*;
import carmadness.controller.*;
import carmadness.view.*;

/**
 * A Game osztály.
 * 
 * @author Szelei
 * @version 1.0
 * @created 10-Apr-2010 2:32:39 PM
 */
public class Game
{
	private GameState _state;
	// controller
	private Controller _controller;
	// modell
	private static Game _instance;

	/**
	 * Játék beállításokat tároló osztály.
	 * 
	 * @author Ujhelyi
	 */
	private class GameConfiguration
	{
		public String mapBuildFilePath = "";

		public FileInputStream getMapFileStream() throws FileNotFoundException
		{
			return new FileInputStream( mapBuildFilePath );
		}
	}

	public static Game getInstance()
	{
		if ( _instance == null ) _instance = new Game();

		return _instance;
	}

	public static void main( String[] args )
	{
		GameConfiguration config = getInstance().new GameConfiguration();

		if ( args.length > 0 ) config.mapBuildFilePath = args[0];

		getInstance().init( config );
	}

	/**
	 * Inicializálás
	 */
	public void init( GameConfiguration config )
	{
		GraphicFrontend frontend = null;
		GraphicController controller = null;

		while ( true )
		{
			Facade.getInstance().reset();

			try
			{
				// Pálya betöltése.
				new ConsoleController( config.getMapFileStream() );
			}
			catch ( FileNotFoundException e )
			{
				System.out.println( e );
			}

			Renderer.registerRenderer( Civilian.class, new GraphicCivilianRenderer() );
			Renderer.registerRenderer( Police.class, new GraphicPoliceRenderer() );
			Renderer.registerRenderer( Robber.class, new GraphicRobberRenderer() );

			Renderer.registerRenderer( RoadSegment.class, new GraphicRSRenderer() );
			Renderer.registerRenderer( SourceRoadSegment.class, new GraphicSourceRSRenderer() );
			Renderer.registerRenderer( DrainRoadSegment.class, new GraphicDrainRSRenderer() );
			Renderer.registerRenderer( Bank.class, new GraphicBankRenderer() );
			Renderer.registerRenderer( PoliceStation.class, new GraphicPoliceStationRenderer() );
			Renderer.registerRenderer( HideOut.class, new GraphicHideOutRenderer() );
			Renderer.registerRenderer( Grass.class, new GraphicGrassRenderer() );

			Renderer.registerRenderer( IncomingRoadSegmentWrapper.class, new GraphicIncomingRSWRenderer() );
			Renderer.registerRenderer( OutgoingRoadSegmentWrapper.class, new GraphicOutgoingRSWRenderer() );

			Renderer.registerRenderer( StopSign.class, new GraphicStopSignRenderer() );
			Renderer.registerRenderer( TrafficLight.class, new GraphicTrafficLightRenderer() );
			Renderer.registerRenderer( Bunny.class, new GraphicBunnyRenderer() );

			Renderer.registerRenderer( SpeedLimit.class, new EmptyRenderer() );

			getInstance()._state = GameState.Playing;

			// view példányosítása, átadása a kontrollernek
			if ( frontend == null )
			{
				frontend = new GraphicFrontend();
				controller = new GraphicController( frontend );
			}

			controller.run();
		}
	}

	/**
	 * Get _state.
	 * 
	 * @return _state as GameState.
	 */
	public GameState getState()
	{
		return _state;
	}

	/**
	 * Set _state.
	 * 
	 * @param _state
	 *            the value to set.
	 */
	public void setState( GameState state )
	{
		this._state = state;
	}
}
